package com.xiongwei.manageback.exception;

import cn.dev33.satoken.util.SaResult;
import com.xiongwei.manageback.utils.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 如果抛出的是ServiceException，调用该方法
     * @param e 业务异常
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public AjaxResult handle(ServiceException e){
        return AjaxResult.fail(e.getMessage(),e.getCode());
    }

    /**
     * 鉴权相关异常全局拦截器
     */
    @ExceptionHandler
    public SaResult handlerException(Exception e){
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }
}
